package cn.net.isir.service2;

import cn.net.isir.dao.MinipIndexDataDao;
import cn.net.isir.dao.ShopDao;
import cn.net.isir.entity.Shop;
import cn.net.isir.entity.TimeRange;
import cn.net.isir.utils.DateUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liuxubo
 * @filename TotalFlowShopStatisticsService.java
 * @package cn.net.isir.service
 * @email lxb@isir.net.cn
 * @date 2019/03/11
 * @comment
 * @since 1.8
 */
@Service
public class TotalFlowShopStatisticsService {
    @Resource
    private MinipIndexDataDao minipIndexDataDao;
    @Resource
    private ShopDao shopDao;

    /**
     * 店铺营业额统计
     *
     * @param salesman     销售人员ID
     * @param turnedShpIds 以转结店铺Id集合
     * @param pageNo
     * @param pageSize
     * @return
     */
    public JSONObject getTotalFlowShop(Map<String, List<TimeRange>> salesman,String name, String turnedShpIds, Integer pageNo, Integer pageSize) {
        StringBuilder sb = new StringBuilder();
        salesman.forEach((key, value) -> sb.append(key).append(","));
        String sales = "";
        if (sb.length() > 0) {
            sales = sb.substring(0, sb.length() - 1);
        }
        PageInfo<Shop> pageInfo = minipIndexDataDao.queryHasNotTurnedShop(sales, name, turnedShpIds, pageNo, pageSize);
        // 声明返回对象
        JSONObject jObject = JSONObject.parseObject(JSON.toJSONString(pageInfo));
        JSONArray jArray = new JSONArray();
        pageInfo.getList().forEach(item -> {
            BigDecimal monthFlow = minipIndexDataDao.queryShopIntervalFlow(salesman.get(item.getSalesmanCode()), item.getId(),
                    DateUtils.getCurrentMonthStartTime(), DateUtils.getCurrentMonthEndTime());
            BigDecimal quarterFlow = minipIndexDataDao.queryShopIntervalFlow(salesman.get(item.getSalesmanCode()), item.getId(),
                    DateUtils.getCurrentQuarterStartTime(), DateUtils.getCurrentQuarterEndTime());
            BigDecimal yearFlow = minipIndexDataDao.queryShopIntervalFlow(salesman.get(item.getSalesmanCode()), item.getId(),
                    DateUtils.getCurrentYearStartTime(), DateUtils.getCurrentYearEndTime());
            JSONObject subJO = JSONObject.parseObject(JSON.toJSONString(item));
            subJO.put("monthFlow", monthFlow);
            subJO.put("quarterFlow", quarterFlow);
            subJO.put("yearFlow", yearFlow);
            jArray.add(subJO);
        });
        jObject.put("list", jArray);
        return jObject;
    }

    /**
     * 查询当月每日的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getDaysFlowAtMonth(Map<String, List<TimeRange>> salesman, String shopId) {
        Calendar calendar = Calendar.getInstance();
        int maxDate = calendar.getActualMaximum(Calendar.DATE);
        String startTime = new SimpleDateFormat("yyyy-MM-## 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-MM-## 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每一天的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= maxDate; i++) {
            String dateStr = String.valueOf(i);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr);
            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);

                dateMap.put(dateStr, minipIndexDataDao.queryShopIntervalFlow(
                        salesman.get(shopDao.selectByPrimaryKey(shopId).getSalesmanCode()), shopId, sDate, eDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }

    /**
     * 查询当年每月的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getMonthsFlowAtYear(Map<String, List<TimeRange>> salesman, String shopId) {
        Calendar calendar = Calendar.getInstance();
        String startTime = new SimpleDateFormat("yyyy-##-01 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-##-#2#2 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每月的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= 12; i++) {
            String dateStr = String.valueOf(i);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr);
            // 计算本月的做大日期
            calendar.set(Calendar.MONTH, i - 1);
            int maxDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            realEtime = realEtime.replace("#2#2", String.valueOf(maxDate));

            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);
                dateMap.put(dateStr, minipIndexDataDao.queryShopIntervalFlow(
                        salesman.get(shopDao.selectByPrimaryKey(shopId).getSalesmanCode()), shopId, sDate, eDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }

    /**
     * 查询年每季度的流水
     *
     * @return 结果
     */
    public Map<String, BigDecimal> getQuarterFlowAtYear(Map<String, List<TimeRange>> salesman, String shopId) {
        Calendar calendar = Calendar.getInstance();
        String startTime = new SimpleDateFormat("yyyy-##-01 00:00:00").format(calendar.getTime());
        String endTime = new SimpleDateFormat("yyyy-##-#2#2 23:59:59").format(calendar.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取每月的数据
        Map<String, BigDecimal> dateMap = new LinkedHashMap<>();
        for (int i = 1; i <= 12; i += 3) {
            String dateStr = String.valueOf(i);
            String dateStr2 = String.valueOf(i + 2);
            if (i < 10) {
                dateStr = "0" + dateStr;
            }
            if (i + 2 < 10) {
                dateStr2 = "0" + dateStr2;
            }
            String realStime = startTime.replace("##", dateStr);
            String realEtime = endTime.replace("##", dateStr2);
            // 计算本月的做大日期
            calendar.set(Calendar.MONTH, i + 2 - 1);
            int maxDate = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            realEtime = realEtime.replace("#2#2", String.valueOf(maxDate));

            try {
                Date sDate = sdf.parse(realStime);
                Date eDate = sdf.parse(realEtime);
                dateMap.put(dateStr + "-" + dateStr2, minipIndexDataDao.queryShopIntervalFlow(
                        salesman.get(shopDao.selectByPrimaryKey(shopId).getSalesmanCode()), shopId, sDate, eDate));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dateMap;
    }
}
